/** Mode handler that listens for the menu action and opens the main menu.
 *@param buffer The buffer to render the main menu to.
 */
function MainMenuMode(buffer)
{
	this.buffer = buffer;
}
MainMenuMode.prototype =
{
	/** Handles an input action.
	 */
	handleAction: function(action)
	{
		switch(action)
		{
			case "menu":
				dhack.addModeHandler(
					new MenuListMode(
						this.buffer,
						15,
						null,
						[
							"Resume Game",
							"Help Library",
							"Save and Quit",
							"Quit Game"
						],
						this._menuCallback,
						this,
						false,
						true
					)
				);
				break;
			default: return false; break;
		}
		return true;
	},
	/** Callback for the main menu.
	 *@param option The option selected.
	 *@param source The MenuListHandler that called the callback.
	 */
	_menuCallback: function(option, source)
	{
		switch(option)
		{
			case "Resume Game":
				source.close();
				break;
			case "Help Library":
				dhack.addModeHandler(new HelpLibraryMode(this.buffer));
				break;
			case "Save and Quit":
				if(dhack.saveGame())
					dhack.enterTitleMode();
				break;
			case "Quit Game":
				var message;

				if(dhack.isSaveAvailable())
					message = "Are you sure you want to quit your game and delete the save file?";
				else
					message = "Are you sure you want to quit? Your progress will not be saved.";
				dhack.addModeHandler(
					new MenuListMode(
						this.buffer,
						30,
						message,
						[
							"No",
							"Yes"
						],
						this._quitConfirmCallback,
						this,
						false,
						false
					)
				);
				break;
			default:
				source.close();
				break;
		}
	},
	/** Callback for the quit confirmation message
	 *@param option The option selected.
	 *@param source The MenuListHandler that called the callback.
	 */
	_quitConfirmCallback: function(option, source)
	{
		switch(option)
		{
			case "Yes":
				dhack.deleteSaveFile();
				dhack.enterTitleMode();
				break;
		}
	}
};
